//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// One-way roads arrows.
//

# include "StdAfx.h"
# include "TypesDraw.h"
# include "Position.h"
# include "fast_sqrt.h"
# include "fast_floor.h"
# include "Globals.h"

const int c_nDirIndicator_a = 8;
const int c_nDirIndicator_b = 3;
const int c_nDirIndicator_Margin = c_nDirIndicator_a;
const int c_nDirIndicator_MinChain = c_nDirIndicator_Margin + c_nDirIndicator_a + 1;
const int c_nDirIndicator_Step = 70;

void DrawDirIndicator (HDC _hDC, const POINT * _pts, size_t _cPoints, const RECT * _pRect, bool _bNoMinChain) {
	::SelectObject (_hDC, g_hpenBlackThin);
	::SelectObject (_hDC, g_hbrGray);

	float l = c_nDirIndicator_Step;
	for (int n = _cPoints - 1; n >= 1; -- n) {
		const POINT & p0 = _pts [n - 1];
		const POINT & p1 = _pts [n];

		const long lDx = p1.x - p0.x;
		const long lDy = p1.y - p0.y;
		if (lDx == 0 && lDy == 0)
			continue;

		const float d = ::labs (lDx) + ::labs (lDy) < 32000 ? isqrt (lDx*lDx + lDy*lDy) : ::hypot ((float) lDx, (float) lDy);
		if (l < c_nDirIndicator_Step || d < (_bNoMinChain ? 3 : c_nDirIndicator_MinChain)) {
			l += d;
			continue;
		}
		l = 0;

		const float fCos = lDx/d;
		const float fSin = lDy/d;

		POINT pt0;
		if (_bNoMinChain) {
			pt0.x = (p0.x + p1.x)/2;
			pt0.y = (p0.y + p1.y)/2;
		} else {
			pt0.x = p1.x + fast_floor (- c_nDirIndicator_Margin*fCos + .5f);
			pt0.y = p1.y + fast_floor (- c_nDirIndicator_Margin*fSin + .5f);
		}
		if (_pRect != NULL && ! ::PtInRect (_pRect, pt0))
			continue;

		POINT points [3];
		points [0]   = pt0;
		points [1].x = pt0.x + fast_floor (- c_nDirIndicator_a*fCos - c_nDirIndicator_b*fSin + .5f);
		points [1].y = pt0.y + fast_floor (- c_nDirIndicator_a*fSin + c_nDirIndicator_b*fCos + .5f);
		points [2].x = pt0.x + fast_floor (- c_nDirIndicator_a*fCos + c_nDirIndicator_b*fSin + .5f);
		points [2].y = pt0.y + fast_floor (- c_nDirIndicator_a*fSin - c_nDirIndicator_b*fCos + .5f);
		::Polygon (_hDC, points, 3);
	}
}
